(function() {
    var G = {
        //apiurl: 'http://sdk.guerlab.net/', //测试服
        apiurl: 'http://www.sharestock.cn/', //测试服
        // apiurl: 'http://192.168.31.10:8080/',
        uploadurl: 'http://upload.sharestock.cn',
        defaultAvator: 'public/images/sdk_user_icon.png',
        defaultCover: 'public/images/sdk_image.jpg',
        get: function(url) {
            var data = arguments[1] || {};
            var success = arguments[2] || null;
            var error = arguments[3] || null;
            var headers = arguments[4] || {};
            G.ajax(url, 'GET', data, success, error, headers);
        },
        post: function(url) {
            var data = arguments[1] || {};
            var success = arguments[2] || null;
            var error = arguments[3] || null;
            var headers = arguments[4] || {};
            G.ajax(url, 'POST', data, success, error, headers);
        },
        put: function(url) {
            var data = arguments[1] || {};
            var success = arguments[2] || null;
            var error = arguments[3] || null;
            var headers = arguments[4] || {};
            G.ajax(url, 'PUT', data, success, error, headers);
        },
        delete: function(url) {
            var data = arguments[1] || {};
            var success = arguments[2] || null;
            var error = arguments[3] || null;
            var headers = arguments[4] || {};
            G.ajax(url, 'DELETE', data, success, error, headers);
        },
        ajax: function(url, type, data, success, error, headers) {
            if (typeof url != "string" || url.length == 0) {
                throw new Error("Please enter the URL");
            }
            if (error == undefined) {
                error = function(message, errorCode) {
                    console.log("fail[url=" + url + ", message=" + message + ", errorCode=" + errorCode + "]");
                    if(errorCode === 401){
                        if(location.href.match('http://www.sharestock.cn/m').length > 0){
                            //移动端
                            location.href = '/m/login.html';
                        }else{
                            layer.msg(message);
                            loginModel()
                        }
                    }
                }
            }

            var requestParams = {
                url: G.apiurl + url,
                data: data,
                type: type,
                headers: {
                    "TOKEN": G.getCookie('jwt')
                },
                success: function(result) {
                    if (typeof result == "string") {
                        result = JSON.parse(result);
                    }

                    if (!result.status) {
                        if (typeof error == "function") {
                            error(result.message, result.errorCode);
                        }

                        return;
                    }
                    if (typeof success == "function") {
                            success(result.data);
                    }
                },
                error: function(e) {
                    if (typeof error != "function") {
                        return;
                    }
                    var text = e.responseText;
                    if (text.length == 0) {
                        return;
                    }
                    try {
                        var responseJson = JSON.parse(text);
                        if (typeof responseJson.message == "string") {
                            error(responseJson.message, responseJson.errorCode);
                        } else {
                            error("系统繁忙。请稍后再试");
                        }
                    } catch (e) {
                        error("系统繁忙。请稍后再试");
                    }
                }
            };
            if (data instanceof FormData) {
                requestParams.processData = false;
                requestParams.contentType = false;
            }

            if (typeof data == 'string') {
                requestParams.contentType = 'application/json';
            }
            if(headers && typeof headers == 'object'){
                for(var k in headers){
                    requestParams.headers[k] = headers[k]
                }
            }
            // for(var j in requestParams.headers){

            //     alert(j + '=' + requestParams.headers[j])
            // }

            $.ajax(requestParams);
        },
        getCookie: function(name){
        	var reg = new RegExp("(^| )" + name + "(?:=([^;]*))?(;|$)");
    		var val = document.cookie.match(reg);
    		return val ? (val[2] ? unescape(val[2]) : "") : null;
        },
        setCookie: function(name,value,expires,path,domain,secure){
		    var exp = new Date();
		    var expires = expires || null;
		    var path = path || "/";
		    var domain = domain || null; 
		    var secure = secure || false;
		    expires ? exp.setMinutes(exp.getMinutes() + parseInt(expires)) : "";
		    document.cookie = name + '=' + escape(value) + (expires ? ';expires=' + exp.toGMTString() : '') + (path ? ';path=' + path : '') + (domain ? ';domain=' + domain : '') + (secure ? ';secure' : '');
		},
		deleteCookie: function(name,path,domain,secure){
		    var path = path || "/";
		    var domain = domain || null; 
		    var secure = secure || false;
		    var value = this.getCookie(name);
		    if (value != null) {
		        var exp = new Date();
		        exp.setMinutes(exp.getMinutes() - 1000);
		        path = path || "/";
		        document.cookie = name + '=;expires=' + exp.toGMTString() + (path ? ';path=' + path : '') + (domain ? ';domain=' + domain : '') + (secure ? ';secure' : '');
		    }
		},
		getQuery: function(name) {
	        var u = arguments[1] || window.location.search;
	        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	        var r = u.substr(u.indexOf("\?") + 1).match(reg);
	        return r != null ? r[2] : "";
	    }
    }
    if (!window.G) {
        window.G = G;
    }

    
})()